package com.yima.baseproj.module.app.gentable.column;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.yima.baseproj.module.app.gentable.column.model.AppGenTableColumnAddParamModel;
import com.yima.baseproj.module.app.gentable.util.StringUtils;
import com.yima.baseproj.module.app.gentable.column.model.AppGenTableColumnQueryTypeEnum;
import com.yima.baseproj.module.config.BaseEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@TableName("app_gen_table_column")
public class AppGenTableColumnEntity extends BaseEntity {

    @ApiModelProperty(value = "归属表编号")
    @TableField(value = "gen_table_id")
    private String genTableId;

    @ApiModelProperty(value = "列名称")
    @TableField(value = "column_name")
    private String columnName;

    @ApiModelProperty(value = "列描述")
    @TableField(value = "column_comment")
    private String columnComment;

    @ApiModelProperty(value = "列类型")
    @TableField(value = "column_type")
    private String columnType;

    @ApiModelProperty(value = "JAVA类型")
    @TableField(value = "java_type")
    private String javaType;

    @ApiModelProperty(value = "JAVA字段名")
    @TableField(value = "java_field")
    private String javaField;

    @ApiModelProperty(value = "是否为插入字段（1是）")
    @TableField(value = "is_insert")
    private Boolean isInsert;

    @ApiModelProperty(value = "是否列表字段（1是）")
    @TableField(value = "is_list")
    private Boolean isList;

    @ApiModelProperty(value = "是否查询字段（1是）")
    @TableField(value = "is_query")
    private Boolean isQuery;

    @ApiModelProperty(value = "查询方式（等于、不等于、大于、小于、范围）")
    @TableField(value = "query_type")
    private String queryType;

    @ApiModelProperty(value = "是否必填")
    @TableField(value = "is_required")
    private Boolean isRequired;

    @ApiModelProperty(value = "显示类型（文本框、文本域、下拉框、复选框、单选框、日期控件）")
    @TableField(value = "html_type")
    private String htmlType;

    @ApiModelProperty(value = "字典类型")
    @TableField(value = "dict_type")
    private String dictType;

    @ApiModelProperty(value = "排序")
    @TableField(value = "sort")
    private Integer sort;

    @TableField(exist = false)
    @ApiModelProperty(value = "JAVA字段名-首字母大写")
    private String javaFieldCapitalize;

    public String getJavaFieldCapitalize() {
        return org.apache.commons.lang3.StringUtils.capitalize(javaField);
    }

    public boolean isSuperColumn() {
        return isSuperColumn(this.javaField);
    }

    public String getQueryTypeShow() {
        return AppGenTableColumnQueryTypeEnum.valueOf(this.queryType).getPlaceholder();
    }

    public static boolean isSuperColumn(String javaField) {
        return StringUtils.equalsAnyIgnoreCase(javaField,
                // BaseEntity
                "id","createBy", "createTime", "updateBy", "updateTime", "delFlag");
    }

    public boolean isUsableColumn() {
        return isUsableColumn(javaField);
    }

    public static boolean isUsableColumn(String javaField) {
        // isSuperColumn()中的名单用于避免生成多余Domain属性，若某些属性在生成页面时需要用到不能忽略，则放在此处白名单
        return StringUtils.equalsAnyIgnoreCase(javaField, "parentId", "orderNum", "remark");
    }

    public void setData(AppGenTableColumnAddParamModel paramModel) {
        this.genTableId = paramModel.getGenTableId();
        this.columnName = paramModel.getColumnName();
        this.columnComment = paramModel.getColumnComment();
        this.columnType = paramModel.getColumnType();
        this.javaType = paramModel.getJavaType();
        this.javaField = paramModel.getJavaField();
        this.isInsert = paramModel.getIsInsert();
        this.isList = paramModel.getIsList();
        this.isQuery = paramModel.getIsQuery();
        this.isRequired = paramModel.getIsRequired();
        this.queryType = paramModel.getQueryType();
        this.htmlType = paramModel.getHtmlType();
        this.dictType = paramModel.getDictType();
        this.sort = paramModel.getSort();
    }
}